package cn.itcast.controller.company;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.company.Company;
import cn.itcast.service.company.CompanyService;
import cn.itcast.vo.PageBean;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.UUID;

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {
    @Reference
    private CompanyService companyService;

//    访问当前方法时，当前登录人必须要有 “企业管理”权限
    @RequiresPermissions("企业管理")
    @RequestMapping(value = "/list",name = "分页展示企业数据列表展示") // page代表当前的页码  page=2
    public String findAll(@RequestParam(value = "page",defaultValue = "1") Integer pageNum , @RequestParam(defaultValue = "10")Integer pageSize){
//        List<Company> companyList = companyService.findAll();
//        PageBean pageBean = companyService.findPage(pageNum,pageSize);
        PageInfo pageInfo = companyService.findPage(pageNum,pageSize);
        request.setAttribute("page",pageInfo); //这里的page名称不能改，因为页面要的就是page这个名称
        return "company/company-list";
    }

    @RequestMapping(value = "/toAdd",name = "跳转到新增企业页面")
    public String toAdd(){

        return "company/company-add";
    }

    @RequestMapping(value = "/edit",name = "保存企业信息")
    public String edit(Company company){

        if(StringUtils.isNotEmpty(company.getId())){
//           id不为空，应该执行修改方法
            companyService.update(company);
        }else{
            company.setId(UUID.randomUUID().toString());
            companyService.save(company);
        }
        return "redirect:/company/list.do"; // 重定向到列表页面
    }
    @RequestMapping(value = "/toUpdate",name = "跳转到修改企业页面")
    public String toUpdate(String id){
        Company company = companyService.findById(id);
        request.setAttribute("company",company);
//        新增和修改页面共用一个
        return "company/company-add";
    }
    @RequestMapping(value = "/delete",name = "删除企业数据")
    public String delete(String id){
        companyService.deleteById(id);
        return "redirect:/company/list.do"; // 重定向到列表页面
    }
    @RequestMapping(value = "/deleteByIds",name = "删除企业数据")
    public String deleteByIds(String[] ids){

        for (String id : ids) {
            companyService.deleteById(id);
        }

        return "redirect:/company/list.do"; // 重定向到列表页面
    }

    public static void main(String[] args) {
        double a = 0.2D;

        System.out.println(a);
    }

}
